package com.hlkj.baojin.constant.request;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Field;


/**
 * 自定义的拦截器,
 * Created by lzx on 2017/6/19 0019.
 */
@Component
public class ConstantInterceptor implements HandlerInterceptor {
    private static Field requestField;

    private static Field parametersParsedField;

    private static Field coyoteRequestField;

    private static Field parametersField;

    private static Field hashTabArrField;

    private Field[] declaredFields = null;

    /**
     * 该方法将在请求处理之前进行调用，只有该方法返回true，才会继续执行后续的Interceptor和Controller，
     * 当返回值为true 时就会继续调用下一个Interceptor的preHandle 方法，如果已经是最后一个Interceptor的时候就会是调用当前请求的Controller方法；
     *
     * @return true 才能继续
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws IOException {
//        //过滤垃圾url  拦截系统之内的路径
//        Arrays.asList(MbxtConstant.REQUEST_URL).forEach(s -> {
//            if(request.getRequestURI().substring(0, s.length()).equals(s)){
//                request.setAttribute("requestPath",true);
//            }
//        });
//        if( request.getAttribute("requestPath")==null){
//            response.sendError(404);
//        }

//        try {
//            Class clazz = Class.forName("org.apache.catalina.connector.RequestFacade");
//            requestField = clazz.getDeclaredField("request");
//            requestField.setAccessible(true);
//
//            parametersParsedField = requestField.getType().getDeclaredField("parametersParsed");
//            parametersParsedField.setAccessible(true);
//
//            coyoteRequestField = requestField.getType().getDeclaredField("coyoteRequest");
//            coyoteRequestField.setAccessible(true);
//
//            String name = request.getClass().getName();
//            Method[] declaredMethods = request.getClass().getDeclaredMethods();
//            Method method = request.getClass().getDeclaredMethod("create" , ServletRequest.class , ServletResponse.class);
//            Object invoke = method.invoke(request, request, response);
//
//            Object innerRequest = requestField.get(invoke);
//            parametersParsedField.setBoolean(innerRequest, true);
//            Object coyoteRequestObject = coyoteRequestField.get(innerRequest);
//            Object parameterObject = parametersField.get(coyoteRequestObject);
//
//
//            parametersField = coyoteRequestField.getType().getDeclaredField("parameters");
//            parametersField.setAccessible(true);
//
//            hashTabArrField = parametersField.getType().getDeclaredField("paramHashStringArray");
//            hashTabArrField.setAccessible(true);
//        } catch (Exception e) {
//            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
//        }
//

        return true;
    }

    /**
     * 该方法将在请求处理之后，DispatcherServlet进行视图返回渲染之前进行调用，可以在这个方法中对Controller 处理之后的ModelAndView 对象进行操作。
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
    }

    /**
     * 该方法也是需要当前对应的Interceptor的preHandle方法的返回值为true时才会执行，该方法将在整个请求结束之后，也就是在DispatcherServlet 渲染了对应的视图之后执行。用于进行资源清理。
     */
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
    }


}




